﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Web;

namespace EAEcoreAddin.SQLWrapperClasses
{
    /*
     *Created by Alexander Schleich
     *for EMoflon:
     *http://www.moflon.org/emoflon/
     */
    class EAElement : EA.Element
    {
        String sqlStringOfElement;
        SQLRepository repository;
        public EAElement(SQLRepository repository, String sqlStringOfElement)
        {
            this.sqlStringOfElement = sqlStringOfElement;
            this.repository = repository as SQLRepository;
        }
        #region IDualElement Members

        String abstractStr = "";
        public string Abstract
        {
            get
            {
                if (abstractStr != "")
                    return abstractStr;
                else
                {
                    abstractStr = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Abstract")[0];
                    return abstractStr;
                }
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string ActionFlags
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        String alias = "";
        public string Alias
        {
            get
            {
                if (alias != "")
                    return alias;
                else
                {
                    alias = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Alias")[0];
                    return alias;
                }
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool ApplyGroupLock(string aGroupName)
        {
            throw new NotImplementedException();
        }

        public bool ApplyUserLock()
        {
            throw new NotImplementedException();
        }

        public int AssociationClassConnectorID
        {
            get { throw new NotImplementedException(); }
        }


        EA.Collection attributes = null;
        public EA.Collection Attributes
        {
            get 
            {
                if (attributes != null)
                    return attributes;
                else
                {
                    attributes = new EACollection();
                    String[] attrs = null;
                    if (repository.FullDatabaseCheckout)
                    {
                        attrs = repository.t_attributeParentID.GetValues(ElementID.ToString());
                    }
                    else
                    {
                        String attributeQry = repository.SQLQuery("select * from t_attribute where Object_ID = " + ElementID);
                        attrs = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(attributeQry, "Row").ToArray();
                    }
                    if (attrs == null)
                        return attributes;

                    foreach (String attribute in attrs)
                    {
                        if (attribute != "")
                        {
                            EA.Attribute newAttribute = new EAAttribute(attribute, repository);
                            (attributes as EACollection).AddNew(newAttribute);
                        }
                    }
                    return attributes;
                }
            }
        }

        public EA.Collection AttributesEx
        {
            get { throw new NotImplementedException(); }
        }

        public string Author
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
 
        public EA.Collection BaseClasses
        {
            get 
            {
                EA.Collection baseClasses = new EACollection();
                foreach (EA.Connector actCon in Connectors)
                {
                    if (actCon.Type == "Generalization")
                    {
                        if (actCon.ClientID == ElementID)
                        {
                            String targetSQLString = "";
                            if (repository.FullDatabaseCheckout)
                            {
                                targetSQLString = repository.t_objectObjectID[actCon.SupplierID.ToString()];
                            }
                            else
                            {
                                String qryString = repository.SQLQuery("select * from t_object where Object_ID = " + actCon.SupplierID);
                                targetSQLString = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(qryString, "Row")[0];
                            }
                            EA.Element baseElement = new EAElement(repository, targetSQLString);
                            (baseClasses as EACollection).AddNew(baseElement);
                        }
                    }
                }
                return baseClasses;
            }
        }

        public int ClassfierID
        {
            get
            {
                if (classifierID != 0)
                    return classifierID;
                else
                    return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Classifier")[0] as String);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        int classifierID = 0;
        public int ClassifierID
        {
            get
            {
                if (classifierID != 0)
                    return classifierID;
                else
                {
                    String result = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Classifier")[0];
                    if (result != "")
                        return int.Parse(result);
                    else
                        return 0;
                }   
            }
            set
            {
                this.classifierID = value;
            }
        }

        public string ClassifierName
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string ClassifierType
        {
            get { throw new NotImplementedException(); }
        }

        public string Complexity
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public object CompositeDiagram
        {
            get { throw new NotImplementedException(); }
        }


        EA.Collection connectors;
        public EA.Collection Connectors
        {
            get 
            {
                if (connectors != null)
                    return connectors;
                else
                {
                    connectors = new EACollection();
                    String[] outgoingLinks = null;
                    String[] incomingLinks = null;
                    if (repository.FullDatabaseCheckout)
                    {
                        outgoingLinks = repository.t_connectorStart_Object_ID.GetValues(ElementID.ToString());
                        incomingLinks = repository.t_connectorEnd_Object_ID.GetValues(ElementID.ToString());
                    }
                    else
                    {
                        String outgoingQry = repository.SQLQuery("select * from t_connector where Start_Object_ID = " + ElementID);
                        outgoingLinks = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(outgoingQry, "Row").ToArray();
                        String incomingQry = repository.SQLQuery("select * from t_connector where End_Object_ID = " + ElementID);
                        incomingLinks = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(incomingQry, "Row").ToArray();
                    }
                    if (incomingLinks != null)
                    {
                        foreach (String actIncomingLink in incomingLinks)
                        {
                            if (actIncomingLink != "")
                            {
                                EA.Connector connector = new EAConnector(repository, actIncomingLink);
                                if (connector.SupplierID != connector.ClientID)
                                    (connectors as EACollection).AddNew(connector);
                            }
                        }
                    }
                    if (outgoingLinks != null)
                    {
                        foreach (String actOutgoingLink in outgoingLinks)
                        {
                            if (actOutgoingLink != "")
                            {
                                EA.Connector connector = new EAConnector(repository, actOutgoingLink);
                                (connectors as EACollection).AddNew(connector);
                            }
                        }
                    }
                    return connectors;
                }
            }
        }

        public EA.Collection Constraints
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection ConstraintsEx
        {
            get { throw new NotImplementedException(); }
        }

        public bool CreateAssociationClass(int ConnectorID)
        {
            throw new NotImplementedException();
        }

        public DateTime Created
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Collection CustomProperties
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection Diagrams
        {
            get 
            {
                EA.Collection diagrams = new EACollection();

                String[] diags = null;
                if (repository.FullDatabaseCheckout)
                {
                    diags = repository.t_diagramParentID.GetValues(ElementID.ToString());
                }
                else
                {
                    String diagsQry = repository.SQLQuery("select * from t_diagram where ParentID = " + ElementID);
                    diags = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(diagsQry, "Row").ToArray();
                }
                if (diags == null)
                    return diagrams;
                foreach (String diag in diags)
                {
                    if (diag != "")
                    {
                        EA.Diagram childDiag = new EADiagram(repository, diag);
                        (diagrams as EACollection).AddNew(childDiag);
                    }
                }
                return diagrams; 
            }
        }

        public string Difficulty
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Collection Efforts
        {
            get { throw new NotImplementedException(); }
        }

        public string ElementGUID
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "ea_guid")[0]; 
            }
        }


        
        public int ElementID
        {
            get 
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement,"Object_ID")[0] as String);
            } 
        }

        EA.Collection elements = null;
        public EA.Collection Elements
        {
            get 
            {
                if (elements != null)
                {
                    return elements;
                }
                else
                {
                    elements = new EACollection();

                    String[] childs = null;

                    if (repository.FullDatabaseCheckout)
                    {
                        childs = repository.t_objectParentID.GetValues(ElementID.ToString());
                    }
                    else
                    {
                        String childsQry = repository.SQLQuery("select * from t_object where ParentID = " + ElementID);
                        childs = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(childsQry, "Row").ToArray();
                    }
                    if (childs == null)
                        childs = new String[0];
                    foreach (String child in childs)
                    {
                        if (child != "")
                        {
                            EA.Element childElement = new EAElement(repository, child);
                            (elements as EACollection).AddNew(childElement);
                        }
                    }
                    
                    return elements;
                }
                
            }
        }

        public EA.Collection EmbeddedElements
        {
            get { throw new NotImplementedException(); }
        }

        public string EventFlags
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string ExtensionPoints
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Collection Files
        {
            get { throw new NotImplementedException(); }
        }

        public string Genfile
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        String genLinks = "";
        public string Genlinks
        {
            get
            {
                if (genLinks != "")
                    return genLinks;
                else
                {
                    genLinks = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Genlinks")[0];
                    return genLinks;
                }
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Gentype
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string GetLastError()
        {
            throw new NotImplementedException();
        }

        public string GetLinkedDocument()
        {
            throw new NotImplementedException();
        }

        public string GetRelationSet(EA.EnumRelationSetType Type)
        {
            throw new NotImplementedException();
        }

        public string GetStereotypeList()
        {
            throw new NotImplementedException();
        }

        public object Header1
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public object Header2
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsActive
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsAssociationClass()
        {
            throw new NotImplementedException();
        }

        public bool IsComposite
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsLeaf
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsNew
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool IsSpec
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Collection Issues
        {
            get { throw new NotImplementedException(); }
        }

        public bool LoadLinkedDocument(string FileName)
        {
            throw new NotImplementedException();
        }

        public bool Locked
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string MetaType
        {
            get
            {
                return "";
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        EA.Collection methods = null;
        public EA.Collection Methods
        {
            get 
            {
                if (methods != null)
                    return methods;
                else
                {
                    methods = new EACollection();

                    String[] ops = null;
                    if (repository.FullDatabaseCheckout)
                    {
                        ops = repository.t_operationParentID.GetValues(ElementID.ToString());
                    }
                    else
                    {
                        String opsQry = repository.SQLQuery("select * from t_operation where Object_ID = " + ElementID);
                        ops = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(opsQry, "Row").ToArray();
                    }

                    if (ops == null)
                        return methods;

                    foreach (String op in ops)
                    {
                        if (op != "")
                        {
                            EA.Method newMethod = new EAMethod(op, repository);
                            (methods as EACollection).AddNew(newMethod);
                        }
                    }
                    return methods;
                }
            }
        }

        public EA.Collection MethodsEx
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection Metrics
        {
            get { throw new NotImplementedException(); }
        }

        public DateTime Modified
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Multiplicity
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        String name = "";
        public string Name
        {
            get
            {
                if (name != "")
                    return name;
                else
                {
                    name = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Name")[0] as String;
                    return name;
                }
            } 
            set 
            {
                throw new NotSupportedException();
            }
        }

        public string Notes
        {
            get
            {
                return HttpUtility.HtmlDecode(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Note")[0]);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.ObjectType ObjectType
        {
            get { return EA.ObjectType.otElement; }
        }

        public int PackageID
        {
            get
            {
                String packageID = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Package_ID")[0];
                if (packageID != "")
                    return int.Parse(packageID);
                else
                    return 0;
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        int parentID;
        public int ParentID
        {
            get
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "ParentID")[0]);
            }
            set
            {
                this.parentID = value;
            }
        }

        public EA.Collection Partitions
        {
            get { throw new NotImplementedException(); }
        }

        public string Persistence
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Phase
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Priority
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Properties Properties
        {
            get { throw new NotImplementedException(); }
        }

        public int PropertyType
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public EA.Collection Realizes
        {
            get { throw new NotImplementedException(); }
        }

        public void Refresh()
        {
            throw new NotImplementedException();
        }

        public bool ReleaseUserLock()
        {
            throw new NotImplementedException();
        }

        public EA.Collection Requirements
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection RequirementsEx
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection Resources
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection Risks
        {
            get { throw new NotImplementedException(); }
        }

        public string RunState
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool SaveLinkedDocument(string FileName)
        {
            throw new NotImplementedException();
        }

        public EA.Collection Scenarios
        {
            get { throw new NotImplementedException(); }
        }

        public void SetAppearance(int Scope, int Item, int Value)
        {
            throw new NotImplementedException();
        }

        public EA.Collection StateTransitions
        {
            get { throw new NotImplementedException(); }
        }

        public string Status
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }


         String stereotype = "";
        public string Stereotype
        {
            get
            {
                if (stereotype != "")
                    return stereotype;
                else
                {   
                    stereotype = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Stereotype")[0];
                    return stereotype;
                }
            }
            set
            {
                stereotype = value;
            }
        }

        public string StereotypeEx
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string StyleEx
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public int Subtype
        {
            get
            {
                return int.Parse(EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "NType")[0]);
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool SynchConstraints(string sProfile, string sStereotype)
        {
            throw new NotImplementedException();
        }

        public bool SynchTaggedValues(string sProfile, string sStereotype)
        {
            throw new NotImplementedException();
        }

        public string Tablespace
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Tag
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }
        EA.Collection taggedValues = null;
        public EA.Collection TaggedValues
        {
            get 
            {
                if (taggedValues != null)
                    return taggedValues;
                else
                {
                    taggedValues = new EACollection();

                    String[] tags = null;
                    if (repository.FullDatabaseCheckout)
                    {
                       tags = repository.t_objectpropertiesObject_ID.GetValues(ElementID.ToString());
                    }
                    else
                    {
                        String taggedQry = repository.SQLQuery("select * from t_objectproperties where Object_ID = " + ElementID);
                        tags = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(taggedQry, "Row").ToArray();
                    }
                    if (tags == null)
                        return taggedValues;
                    foreach (String tag in tags)
                    {
                        if (tag != "")
                        {
                            EA.TaggedValue newTag = new EATaggedValue(repository, tag);
                            (taggedValues as EACollection).AddNew(newTag);
                        }
                    }
                    return taggedValues;
                }
            }
        }

        public EA.Collection TaggedValuesEx
        {
            get { throw new NotImplementedException(); }
        }

        public EA.Collection Tests
        {
            get { throw new NotImplementedException(); }
        }


        public EA.Collection Templates
        {
            get
            {
                EA.Collection templates = new EACollection();

                String[] templatesArray = null;
                if (repository.FullDatabaseCheckout)
                {
                    templatesArray = repository.t_xrefElementGUID.GetValues(ElementGUID);
                }
                else
                {
                    String taggedQry = repository.SQLQuery("select * from t_xref where Client = '" + ElementGUID+"'");
                    templatesArray = EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(taggedQry, "Row").ToArray();
                }
                if (templatesArray == null)
                    return templates;
                foreach (String templateEntry in templatesArray)
                {
                    if (templateEntry != "")
                    {
                        EATemplate newTemplate = new EATemplate(repository, templateEntry);
                        (templates as EACollection).AddNew(newTemplate);
                    }
                }
                return templates;

            }
        }


        public int TreePos
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Type
        {
            get
            {
                return EAEcoreAddin.Util.EAUtil.getXMLNodeContentFromSQLQueryString(sqlStringOfElement, "Object_Type")[0];
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public bool UnlinkFromAssociation()
        {
            throw new NotImplementedException();
        }

        public bool Update()
        {
            throw new NotImplementedException();
        }

        public string Version
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string Visibility
        {
            get
            {
                throw new NotImplementedException();
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string get_MiscData(int index)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
